﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MutilLanguage_Search.Controller
{
    class HtmlTagAnalysis
    {
        public static string RemoveVienameseSign(string inputString)
        {
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = inputString.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').Replace(' ', ' ');
        }
        public static string RemoveParentheses(string source)
        {
            //  return Regex.Replace(source, "\\(.*?\\)", string.Empty);
            string result = string.Empty;
            int i = 0;
            foreach (char ch in source)
            {
                if (ch.ToString().Equals("("))
                {
                    i++;
                    continue;
                }
                if (ch.ToString().Equals(")"))
                {
                    i--;
                    continue;
                }
                if (i == 0)
                    result += ch;
            }
            return result;
        }
        public static string[] SlitString(string source)
        {
            string pattern = ",|;";
            Regex myRegex = new Regex(pattern);
            string[] result = myRegex.Split(source);
            return result;
        }
        public static string StripTagsRegex(string source)
        {
            return Regex.Replace(source, "<.*?>", string.Empty);
        }
        static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
        public static string StripTagsRegexCompiled(string source)
        {
            return _htmlRegex.Replace(source, string.Empty);
        }
        //Replace ' to ''
        static Regex symbol = new Regex("'", RegexOptions.Compiled);
        public static string replaceSymbol(string source)
        {
            return symbol.Replace(source, "''");
        }
        public static string StripTagsCharArray(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }
    }
}
